package com.chenyun.boot.datasource.extension;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.chenyun.common.vo.SessionUser;
import com.chenyun.satoken.starter.util.SaUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;

/**
 * mybatis-plus自动填充扩展
 * 注意: 必须调用 mp =>  mapper中的方法才生效,如果是直接走xml的方法不会生效
 * 需要继承并实现 MetaObjectHandler
 * 必须添加到bean容器中
 */
@ConditionalOnClass(MetaObject.class)
@Component
@Slf4j
public class DefaultFieldHandler implements MetaObjectHandler {

    SessionUser getUser() {
        SessionUser user = null;
        try {
            user = SaUtil.getLoginUser() ;
        } catch (Exception exception) {
            log.error("mybatis添加默认值获取用户信息失败,设置默认用户",exception);
            user = new SessionUser().builderSysUserDefault();
        }
        return user;
    }

    /**
     * 插入时的操作
     *
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("mybatis-plus添加时默认值处理...");
        Date date = new Date();
        SessionUser user = getUser();
        String userId = "",tenanId="";
        if (Objects.nonNull(user)) {
            userId = user.getUserId();
            tenanId=user.getClientId();
        }
        log.debug("用户编号:{},当前租户:{}",userId,tenanId);
        if (metaObject.hasSetter("createTime")) {
            log.debug("插入createTime");
            this.fillStrategy (metaObject,"createTime", date );
        }

        if (metaObject.hasSetter("createBy")) {
            log.debug("插入createBy");
            this.fillStrategy(metaObject,"createBy", userId );
        }
        if (metaObject.hasSetter("delFlag")) {
            this.fillStrategy(metaObject,"delFlag", false );
        }
        //将添加的时候将更新设置为一样的
        if (metaObject.hasSetter("updateTime")) {
            log.debug("插入updateTime");
            this.fillStrategy(metaObject,"updateTime", date );
        }
        if (metaObject.hasSetter("updateBy")) {
            log.debug("插入updateBy");
            this.fillStrategy(metaObject,"updateBy", userId );
        }
        if (metaObject.hasSetter("revision")) {
            log.debug("插入revision");
            this.fillStrategy(metaObject,"revision",0L);
        }

        if (metaObject.hasSetter("tenantId")) {
            log.debug("tenantId");
            this.fillStrategy(metaObject,"tenantId", tenanId );
            log.info("tenantId:{}",metaObject.getValue("tenantId"));
        }

    }

    /**
     * 更新时的操作
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("mybatis-plus更新时默认值处理...");
        if (metaObject.hasSetter("updateTime")) {
            log.debug("插入updateTime");
            this.fillStrategy(metaObject,"updateTime", new Date());
        }
        if (metaObject.hasSetter("updateBy")) {
            SessionUser user = getUser();
            String userId = "";
            if (Objects.nonNull(user)) {
                userId = user.getUserId();
            }
            log.debug("插入updateBy");
            this.fillStrategy(metaObject,"updateBy", userId );
        }


    }
}
